package com.docmanage.web.controller.am;

import com.docmanage.am.domain.AmApproval;
import com.docmanage.am.domain.AmBorrow;
import com.docmanage.am.domain.dto.AmApprovalDTO;
import com.docmanage.am.domain.dto.AmBorrowSearchDTO;
import com.docmanage.am.domain.dto.AmBorrowStatisticsDTO;
import com.docmanage.am.domain.vo.AmBorrowSearchVo;
import com.docmanage.am.domain.vo.AmBorrowStatisticsVO;
import com.docmanage.am.service.impl.AmBorrowService;
import com.docmanage.common.annotation.Log;
import com.docmanage.common.core.controller.BaseController;
import com.docmanage.common.core.domain.AjaxResult;
import com.docmanage.common.core.page.TableDataInfo;
import com.docmanage.common.enums.BusinessType;
import com.docmanage.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 档案借阅Controller
 * 
 * @author pointLion
 * @date 2023-06-18
 */
@RestController
@RequestMapping("/am/amBorrow")
public class AmBorrowController extends BaseController
{
    @Autowired
    private AmBorrowService amBorrowService;

    /**
     * 查询档案借阅列表
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmBorrowSearchDTO dto)
    {
        startPage();
        List<AmBorrowSearchVo> list = amBorrowService.selectAmBorrowList(dto);
        return getDataTable(list);
    }

    /**
     * 查询个人档案借阅列表
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:personList')")
    @GetMapping("/personList")
    public TableDataInfo personList(AmBorrowSearchDTO dto)
    {
        startPage();
        List<AmBorrowSearchVo> list = amBorrowService.selectPersonAmBorrowList(dto);
        return getDataTable(list);
    }

    /**
     * 导出档案借阅列表
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:export')")
    @Log(title = "档案借阅", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AmBorrowSearchDTO dto)
    {
        List<AmBorrowSearchVo> list = amBorrowService.selectAmBorrowList(dto);
        ExcelUtil<AmBorrowSearchVo> util = new ExcelUtil<>(AmBorrowSearchVo.class);
        util.exportExcel(response, list, "档案借阅数据");
    }

    /**
     * 获取档案借阅详细信息
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(amBorrowService.selectAmBorrowById(id));
    }

    /**
     * 获取档案借阅详细信息用于审批页数据展示
     */
    @GetMapping(value = "/getApprovalInfo/{id}")
    public AjaxResult getApprovalInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(amBorrowService.getApprovalInfo(id));
    }

    /**
     * 用户提交借阅提交审批
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:submit')")
    @PostMapping(value = "submitBorrow/{id}")
    public AjaxResult submitBorrowForm(@PathVariable("id") Long id)
    {
        return toAjax(amBorrowService.submitBorrowForm(id));
    }

    /**
     * 档案管理员审批借阅单
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:approval')")
    @PostMapping(value = "approval")
    public AjaxResult approvalBorrowForm(@RequestBody AmApprovalDTO amApproval)
    {
        int i = amBorrowService.approvalBorrowForm(amApproval);
        if (i==1||i==0){
            return toAjax(i);
        }else {
            return AjaxResult.error("您不是档案管理员，请联系档案管理员审批！");
        }
    }

    /**
     * 新增档案借阅
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:add')")
    @Log(title = "档案借阅", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AmBorrow amBorrow)
    {
        return toAjax(amBorrowService.insertAmBorrow(amBorrow));
    }


    /**
     * 档案借阅归还
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:return')")
    @Log(title = "档案借阅归还", businessType = BusinessType.UPDATE)
    @PostMapping("return_description")
    public AjaxResult returnDescription(@RequestParam Long id)
    {
        return toAjax(amBorrowService.setReturnTime(id));
    }

    /**
     * 借阅/借出档案
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:borrow')")
    @Log(title = "借阅/借出档案", businessType = BusinessType.UPDATE)
    @PostMapping("borrow_description")
    public AjaxResult borrowDescription(@RequestParam Long id)
    {
        Integer i = amBorrowService.setBorrowTime(id);
        if (i!=0) {
            return AjaxResult.success("操作成功",i);
        }
        return AjaxResult.error("操作失败，档案未在库！");
    }

    /**
     * 借阅/借出档案
     */
    @PostMapping("borrow_statistics")
    public TableDataInfo borrowStatistics(@RequestBody AmBorrowStatisticsDTO dto)
    {
       List<AmBorrowStatisticsVO> list = amBorrowService.borrowStatistics(dto);
        return getDataTable(list);
    }

    /**
     * 修改档案借阅
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:edit')")
    @Log(title = "档案借阅", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AmBorrow amBorrow)
    {
        return toAjax(amBorrowService.updateAmBorrow(amBorrow));
    }


    /**
     * 档案借阅撤销
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:repeal')")
    @Log(title = "档案借阅撤销", businessType = BusinessType.UPDATE)
    @PutMapping("repeal")
    public AjaxResult repeal(@RequestParam Long id)
    {
        return toAjax(amBorrowService.repeal(id));
    }

    /**
     * 删除档案借阅
     */
    @PreAuthorize("@ss.hasPermi('am:amBorrow:remove')")
    @Log(title = "档案借阅", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(amBorrowService.deleteAmBorrowByIds(ids));
    }
}
